from PySide6.QtCore import QDateTime, Slot, Qt
from PySide6.QtWidgets import QMessageBox, QDialog, QTableWidgetItem, QMainWindow, QButtonGroup
from PySide6.QtGui import QIcon, QColor

from Devices.DeviceWindow import DeviceWindow
from Devices.Chiot.BsDevs.ComDev.Forms.ComDevWindow import ComDevWindow
from Devices.Chiot.BsDevs.Sensor.SensorDevice import SensorDevice
from Devices.Chiot.BsDevs.Sensor.SensorParser import SensorParser
from Devices.Chiot.BsDevs.Sensor.Forms.SensorWindow_UI import Ui_FormSensorWindow
from Components.MessageBoxFunc import MessageBoxFunc

class SensorWindow(ComDevWindow):
    # 0、窗体初始化、状态变量初始化
    def __init__(self, supportDevice, parent = None):
        # 注意在实现特有设备时，一定要先将其ui对象加载完毕
        super().__init__(supportDevice, parent)

        self.FullStatus = SensorDevice.SensorFullStatus()

        self._ui = Ui_FormSensorWindow()
        self._ui.setupUi(self)
        self.initUiWindow()
        self.SetupUi(self._ui)
        # self.testUi()

    # 1、数据解析
    def setHeartbeatStatus(self):
        pass

    def CmdRptStatusReceived(self, msgValue):
        if SensorParser.ParseHeartbeat(msgValue, self.FullStatus):
            self.setFullStatus()

    def CmdHeartbeatReceived(self, msgValue):
        if SensorParser.ParseHeartbeat(msgValue, self.FullStatus):
            self.setFullStatus()

    def CmdFullStatusReceived(self, msgValue):
        if SensorParser.ParseHeartbeat(msgValue, self.FullStatus):
            self.setFullStatus()

    def CmdMsgValueReceived(self, msgName, msgValue):
        pass

    # 2、运行状态
    def setFullStatus(self):
        self._ui.label_sensorStatus.setText(str(self.FullStatus.sensorStatusMsg))
        self._ui.label_powerWarn.setText("告警" if self.FullStatus.powerWarn else "未告警")
        self._ui.label_batteryPercentage.setText(str(self.FullStatus.batteryPercentage) + "%")
        self._ui.label_checkStatus.setText("正在自检" if self.FullStatus.checkStatus else "未自检")

        self._ui.label_adcConvertValue.setText(str(self.FullStatus.adcConvert) + " Mv")
        self._ui.label_adcOriginValue.setText(str(self.FullStatus.adcOrigin))
        self._ui.label_adcReferValue.setText(str(self.FullStatus.adcRefer))
        self._ui.lineEdit_adcCalibration.setText(str(self.FullStatus.adcCali))
        self._ui.lineEdit_adcFactor.setText(str(self.FullStatus.adcFactor))
        self._ui.lineEdit_adcIntercept.setText(str(self.FullStatus.adcIntercept))
                              
    @Slot()
    def on_pushButton_setAdcCalibration_clicked(self):
        adcClibrationStr = self._ui.lineEdit_adcCalibration.text().strip()
        if len(adcClibrationStr) == 0:
            self.ShowMessageBox("设置adc参数", "校准值不能为空!", QMessageBox.Critical)
            return
        
        try:
            adcClibration = int(adcClibrationStr)
            msg = SensorParser.PackSetAdcParam("adcCali", adcClibration)
            self.SendCommandText(msg)
        except:
            self.ShowMessageBox("设置adc参数", "系数设置[" + adcClibrationStr + "]错误!", QMessageBox.Critical)

    
    @Slot()
    def on_pushButton_setAdcFactor_clicked(self):
        adcFactorStr = self._ui.lineEdit_adcFactor.text().strip()
        if len(adcFactorStr) == 0:
            self.ShowMessageBox("设置adc参数", "系数不能为空!", QMessageBox.Critical)
            return

        try:
            adcFactor = int(adcFactorStr)        
            msg = SensorParser.PackSetAdcParam("adcFactor", adcFactor)
            self.SendCommandText(msg)
        except:
            self.ShowMessageBox("设置adc参数", "系数设置[" + adcFactorStr + "]错误!", QMessageBox.Critical)
    
    @Slot()
    def on_pushButton_setAdcIntercept_clicked(self):
        adcInterceptStr = self._ui.lineEdit_adcIntercept.text()
        if len(adcInterceptStr) == 0:
            self.ShowMessageBox("设置adc参数", "截距不能为空!", QMessageBox.Critical)
            return

        try:
            adcIntercept = int(adcInterceptStr)
            msg = SensorParser.PackSetAdcParam("adcIntercept", adcIntercept)
            self.SendCommandText(msg)
        except:
            self.ShowMessageBox("设置adc参数", "截距设置[" + adcInterceptStr + "]错误!", QMessageBox.Critical)